package com.yuexun.dhdoctor.module.meetingMaterial;


import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.meetingMaterial.adapter.MeetingMaterialListAdapter;
import com.yuexun.dhdoctor.module.meetingMaterial.bean.MeetingMaterialListBean;
import com.yuexun.dhdoctor.module.meetingMaterial.model.impl.MeetingMaterialModel;
import com.yuexun.dhdoctor.module.meetingMaterial.presenter.IMeetingMaterialPresenter;
import com.yuexun.dhdoctor.module.meetingMaterial.presenter.impl.MeetingMaterialPresenter;
import com.yuexun.dhdoctor.module.meetingMaterial.ui.IMeetingMaterialView;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Act_MeetingMaterialList extends BaseKJActivity implements IMeetingMaterialView {
    @Bind(R.id.filter_edittext)
    EditText filterEdittext;
    @Bind(R.id.meeting_material_list)
    PullToRefreshList meetingMaterialList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    /**
     * 加载会议材料列表状态
     *//*
    public static final int DOWNLOAD_SUCCESS = 1;
    public static final int DOWNLOAD_NODATA = 0;
    public static final int DOWNLOAD_NETERROR = -1;*/

    private ListView mList;
    private int pageSize = 6666;
    private int pageIndex = 1;

    private List<MeetingMaterialListBean.Datas> beanList = new ArrayList<>();
    private MeetingMaterialListAdapter mMeetingMaterialListAdapter;
    private IMeetingMaterialPresenter mMeetingMaterialPresenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_metting_material_list);
        ButterKnife.bind(this);
        mMeetingMaterialPresenter = new MeetingMaterialPresenter(this, new MeetingMaterialModel());
    }

    @Override
    public void initData() {
        super.initData();
        listViewPreference();
        mMeetingMaterialPresenter.showMeetingMaterialList(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID), "",
                "", "", pageSize, pageIndex, true);
//        filterEdittext.setOnKeyListener(onKey);

        filterEdittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    mMeetingMaterialPresenter.showMeetingMaterialList(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID),
                            v.getText().toString(), "", "", pageSize, pageIndex, true);
                }
                return false;
            }
        });

        filterEdittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mMeetingMaterialPresenter.showMeetingMaterialList(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID),
                        filterEdittext.getText().toString(), "", "", pageSize, pageIndex, true);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }


    @Override
    public void showMeetingMaterialList(List<MeetingMaterialListBean.Datas> datasList) {
        if (meetingMaterialList.isPullRefreshEnabled()) {
            meetingMaterialList.onPullDownRefreshComplete();
        }
        beanList.clear();
        beanList = datasList;
        mMeetingMaterialListAdapter = new MeetingMaterialListAdapter(mList, beanList, R.layout.item_meeting_material_list);
        mList.setAdapter(mMeetingMaterialListAdapter);
        emptyLayout.dismiss();
    }



    /*View.OnKeyListener onKey=new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                REFESH_TAG =1;
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("nameOrTitle",filterEdittext.getText().toString());
                params.put("pageIndex",pageIndex+"");
                presenter.getMail(params);
            }
            return false;
        }
    };*/

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = meetingMaterialList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        meetingMaterialList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) meetingMaterialList.getFooterLoadingLayout())
                .setNoMoreDataText("已经是最新状态");
        meetingMaterialList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
//                presenter.showPatientList("");
                mMeetingMaterialPresenter.showMeetingMaterialList(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID),
                        filterEdittext.getText().toString(), "", "", pageSize, pageIndex, true);
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                meetingMaterialList.setHasMoreData(false);
            }
        });


        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(aty, Act_MeetingMaterialDetail.class);
                intent.putExtra("meetingMaterialId", beanList.get(position).getMeetingmaterialid());
                intent.putExtra("fromName", beanList.get(position).getFromname());
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.back)
    public void onClick() {
        finish();
    }


}
